package com.usermanage.ServiceImpl;

import com.usermanage.Mapping.CompanyMapping;
import com.usermanage.Mapping.ContractMapping;
import com.usermanage.Service.CompanyService;
import com.usermanage.Service.ContractService;
import com.usermanage.beanParam.CompanyBean;
import com.usermanage.beanParam.ContractBean;
import com.usermanage.dataDao.JpaDao;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * Created by 過客 on 2018/3/9
 */
@Service("contractService")
public class ContractServiceImpl implements ContractService {

    @Resource
    private ContractMapping contractMapping;
    @Resource
    private JpaDao jpaDao;


    @Override
    public List<ContractBean> findAllList(String key, String page, String size,String company_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT uc.*,ucp.company_name as partyB from u_contract uc LEFT JOIN u_company ucp ON uc.partyB = ucp.id  WHERE 1 = 1  ");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and (contract_name like '%").append(key).append("%' or uc.contract_no LIKE '%").append(key).append("%' or uc.partyA LIKE '%").append(key).append("%' or uc.active_time LIKE '%").append(key).append("%' or uc.end_time LIKE '%").append(key).append("%' or ucp.company_name LIKE '%").append(key).append("%')");
        }
        if(StringUtils.isNotBlank(company_id)){
            buffer.append(" and uc.companyB_id = '").append(company_id).append("' ");
        }
        buffer.append(" order by id desc limit ").append((Integer.valueOf(page)-1) * Integer.valueOf(size)).append(",").append(size);
        List<ContractBean> list = jpaDao.getListByPage(buffer.toString(),ContractBean.class);
        return list;
    }

    @Override
    public int getAllListCount(String key,String company_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) from u_contract uc LEFT JOIN u_company ucp ON uc.partyB = ucp.id  WHERE 1 = 1  ");
        if (StringUtils.isNotBlank(key)){
            buffer.append(" and (contract_name like '%").append(key).append("%' or uc.contract_no LIKE '%").append(key).append("%' or uc.partyA LIKE '%").append(key).append("%' or uc.active_time LIKE '%").append(key).append("%' or uc.end_time LIKE '%").append(key).append("%' or ucp.company_name LIKE '%").append(key).append("%')");
        }
        buffer.append(" and uc.companyB_id = '").append(company_id).append("' ");
        return jpaDao.getListCountByPage(buffer.toString());
    }

    @Override
    public Boolean isExisit(ContractBean bean) {
        Example<ContractBean> example = Example.of(bean);
        return contractMapping.exists(example);
    }

    @Override
    public List<ContractBean> findAll(ContractBean bean) {
        Example<ContractBean> example = Example.of(bean);
        return contractMapping.findAll(example);
    }

    @Override
    public ContractBean save(ContractBean bean) {
        return contractMapping.save(bean);
    }

    @Override
    public Optional<ContractBean> findById(int id) {
        return contractMapping.findById(id);
    }

    /**
     * 根据SPU编号查询对应的有效合同列表
     *
     * @param spu_code
     */
    @Override
    public List<ContractBean> findAllBySpuCode(String spu_code) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT uc.*,ucp.company_name as partyB from u_contract uc LEFT JOIN u_company ucp ON uc.partyB = ucp.id  WHERE 1 = 1 and uc.contract_sku like '%").append(spu_code).append("%'");
        List<ContractBean> list = jpaDao.getListByPage(buffer.toString(),ContractBean.class);
        return list;
    }
}
